/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import org.eclipse.gef.commands.Command;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.IModelerHelper;
import com.cea.papyrus.core.pseudoelement.EnhancedType;
import com.cea.papyrus.core.pseudoelement.UMLElementFactory;
import com.cea.papyrus.core.utils.Debug;

/**
 * This the more abstract command to do an action in Papyrus
 * It look for the good Factory for the class.
 */
public class PapyrusCommand extends Command{

	/**
	 * The imodeler factory.
	 */
	protected IModelerHelper imodelerFactory=null;

	/**
	 * The element class.
	 */
	public Class elementClass=null;

	/**
	 * Factory used to create the element.
	 */
	protected Object elementFactory;

	/**
	 * debug channel.
	 */
	protected final static int channel = 17;

	protected EnhancedType enhancedType;

	/**
	 * The Constructor.
	 * 
	 * @param element the element
	 * @param umlElement the umlelement with which we call a command
	 * It look for the good factory
	 */
	public PapyrusCommand(Object element) {
//		Object is UMLElementFactory when the creation is initiated by 
		// a user defined button in palette
		if (element instanceof EnhancedType) {
			this.enhancedType = (EnhancedType) element;
			if (enhancedType instanceof UMLElementFactory) {
				this.elementClass = ((UMLElementFactory) enhancedType).getClazz();
			} else {
				this.elementClass = null;
			}

		} else {
			this.enhancedType = null;
			this.elementClass         = (Class) element;
		}

		Debug.debug(this,"type of the Element:"+elementClass, channel);
		if (elementClass!=null){
			this.imodelerFactory= PapyrusPlugin.getDefault().getFactoryRegistry().getFactory(this.elementClass);
			this.elementFactory = this.imodelerFactory.getElementFactory(this.elementClass);
		}
	}

	public PapyrusCommand() {

	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public String toString(){
		return "PapyrusCommand (elementClass: "+this.elementClass+", imodelerFactory: "+ this.imodelerFactory+", elementFactory: "+ this.elementFactory+")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}
}
